Computer Programming in C
PARTICIPANTS: Please put your name here if you would like to participate HOMEWORK/DISCUSSION PAGE: http://invisiblecollegeclasses.dyndns.info/Forums/viewforum.php?f=4 ''COMPUTER PROGRAMMING IN C Hello everyone! This class will teach you the language C. Before you learn the language itself, you should probably learn the following things: What is C? *How to get started *Intro to computer programming 'WHAT IS C?' C is one of the earlier computer programming languages made between 1969 1973. It was developed by AT&T Bell Labs. 'WHY DO I WANT TO LEARN C?' a. It is fun to learn (in my opinion) b. Many programming languages are based on it. These include C++, Java, and many others 'HOW TO GET STARTED ''' You should download a program to compile C. Many programs make this easy for you by coloring key words and pointing out errors/warnings. I would higly highly reccomend XCode for all of you Mac users-it is a very large program used for many languages including C. You can download an earlier version of XCode from developer.apple.com, or you can buy XCode 4 (well worth the $4.99 in my opinion) from the Mac App Store. PC users, sorry but you are on your own for as far as finding a good program for editing and compiling C goes, as I have almost no experience with Windows. INTRO TO COMPUTER PROGRAMMING While you learn computer programming, you will find many errors in your code. This is normal, even for experienced programmers. You will also notice that there is a large and generally friendly community out there. There are many websites dedicated to computer programmers such as stackoverflow.com and many others, depending on your language. Also, you will get frustrated many times. Don't give up! Sometimes you delete important stuff, or make big mistakes. Good Luck! VOCABULARY If you are not very experienced with computers, you will be wondering what some of these words mean. If there's a word that you don't know and isn't here, feel free to comment (or just use Google). Some the vocabulary words are: *Compile: Take all the the lines of code and turn them into 1s and 0s that the computer can understand. *Debug: Take all the bugs(bad lines of code that don't work) out. Many compilers will point these bugs out GETTING STARTED(in XCode 4) #Launch XCode #Click Create New XCode Project #On the left side of the menu, select Application under Mac OS X #Click Command Line Tool and press enter #name your project HelloWorld or whatever you want. You will need to write your code accordingly though: replace all instances of HelloWorld in my code to whatever your name is in your code #Choose C from the dropdown list, if it isn't already chosen #Save to whatever directory you want. #Now you must get used to the user interface in XCode This is called the Groups and Files pane. It shows all the files in your project: This is called the Editor. You can click on a file in the the Groups and Files pane and its contents will appear here: Here are some other features: the Output, the warnings/errors tab and the Run button. The output is text-only. This is where your program will run. You may interact with your program through this window, too. The warnings/errors tab will be the errors and warnings returned from the debugger. Clicking on these erros brings you where they are located in your code. Errors will appear in many places other than this tab, and you can click those to be directed to where the error appears also. The run button compiles the code and runs your program in the Output are. HELLO WORLD If you have ever tried to learn any programming language, the first program you ever wrote was most likely something that says 'Hello World'. I will show the contents of a Hello World program and I will explain what it's doing. If you already have HelloWorld open by following the steps under getting started (in XCode 4), ''click on main.c in the groups and files pane. You should see the following: // main.c // HelloWorld // Copyright 2011 __MyCompanyName__. All rights reserved. // #include int main (int argc, const char * argv[]) { //Insert code here... printf("Hello, World!\n") return 0; } Let's begin by explaining this: // main.c // HelloWorld // // Created by Nanak Nihal Khalsa on 3/24/11. // Copyright 2011 __MyCompanyName__. All rights reserved. // These are ''comments. ''Comments are very useful. They are not compiled, so you can type them in your code to make it clear to you and other developers, with out affecting anything in your program. Comment your code a lot; trust me it will help. To comment code, you type // your comment here This type of comment of comment only lasts for one line. For example, if you type // This is a comment. comments are very useful. oops, new line. I'm goi ng to get an error this will give you an error because it considers "ng to get an error" a line of code. You can fix this by typing // This is a comment. comments are very useful. oops, new line. I'm not //going to get an error anymore This can also be accomplished by doing a different type of comment that starts with /* and ends wherever it finds a */. For example, this would be a valid comment: /* This is a comment. comments are very useful. oops, new line. I'm not going to get an error anymore */ #include stdio.h is a file. It is the C Standard Library. This means it has many functions and other things included in it that you can use. For example, the printf() function is in there. You can not use printf() if you don't include the stdio.h file. You will want to include stdio.h in all C programs you use. You will most likely include other files such as time.h and many others. The next bit of code is: int main (int argc, const char * argv[]) { //Insert code here... printf("Hello, World!\n") return 0; } This: int main (int argc, const char * argv[]) will appear in all of your C programs. Everything in there is what gets executed in your program. What I mean by 'in there' is what's in between these brackets: { }In this case, it is: //Insert code here... printf("Hello, World!\n"); return 0; You already know that //Insert code here... is a comment. The line printf("Hello, World!\n"); means that your computer will show the text "Hello, World!". printf() is a function that displays text. the \n is the equivalent of pressing returrn. It creates a new line. There or other 'backslash combinations' such as \" and \t. Make sure you type \ and not /. \ is usually below the delete key. The final line of code is: return 0; This ends the function, in this case main(). Different return values mean differnet things. For more information on return values, use Google. Lastly note the semicolon at the end of many lines. These are used at the end of almost every line. I cannot provide a good explanation of when to use them, but I'm sure there are many websites with that information. They are not used for statements beginning with # and for comments. If you are confused about semicolons, use Google.Finally, run your code by pressing the Run button. You can see it in the output window and you can run the file in Terminal if you prefer. This file will be in your Groups and Files pane. 'VARIABLES:' variables are very important in all programming languages. They store temporary information. Variables can hold things other than numbers such as strings. What do I mean by temporary information? I mean when you create a variable it is not stored on the hard drive (or SSD). Instead they are stored in the RAM or Random Access Memory. RAM is often referred to as memory. It is very fast, but has a small amount of space. Its contents are erased when it is not connected to power. In C, you can write to a file on the hard drive or SSD. You can keep things such as preferences here; as the will not be deleted unless the user chooses to do so. Whenever you declare a variable, it gets saved in the RAM. DECLARING VARIABLES: you can declare variables by doing the following: int myInt = 3; this creates a variable named myInt with a value of 3. Variable names must only contain letters, numbers, and underscores (_). Why is it called an int? This brings us to our new section, Variable Types. VARIABLE TYPES: To understand variable types, one must learn some basics of computers, more specifically, bits and bytes. This may be a ''bit ''confusiong (ha ha ha) at first, but feel free to comment; I don't ''byte(ha ha ha). Each bit (no pun intended, seriously) of data on your computer is made of bytes. Bytes contain either 8, 16, 32, or 64 bits in them as of now. For the sake of not writing out 64 1s and 0s for each byte, I will stick to 8 bit for this tutorial. Certain variable types have certain types of information and a certain amount of bytes. Here are some examples: int myInt = 3; /* this creates an int named myInt with a value of 3. Int stands for integer and contains only positive or negative numbers. Ints contain 4 bytes. This should not be very limiting to you, unless you plan on storing very high numbers, which is possible. Also, since it contains negative numbers, it will only hold an equivalent to 2 bytes of positive numbers */ //this brings us to the concept of unsigned ints. unsigned int myUnsignedInt = 3; /*just like int. It is 4 bytes, but it only contains positive integers, which gives it twice as much space for positive numbers. Also keep in mind that ints do not store decimals. You will not get an error when you set it to a decimal; it will merely get rounded. You can use unsigned variables other than ints, such as long ints, which I will explain later */ long int myLong = 3; /* long ints are just ints, but twice the size. They are 8 bytes. */ /*short ints and unsigned long ints ...this is relatively self-explanatory. Short ints are 2 bytes and unsigned longs are 8 bytes and are unsignes, so they can hold twice as many values as high as 18446744073709551615 */ //floats //The one problem with all these ints is that they only hold non-decimals. float myFloat = 3.14159265; //as you can see, floats can hold decimals. They can also store large values //doubles double myDouble = 3.14159265; double myDouble = 9999999999999999999999999999999999999999999999999999999999; /*doubles are like floats, but bigger. They can store HUGE values, even more that unsinged long ints. If you want a gigantic value, use a long double. Also note that doubles and floats are not very accurate at high values. Here is an example: long double myBigNumber = 9999999999999999999999; this gives a value of '1864712049423024127.000000 '*/ //chars char myChar = 'a'; /* let's say you want to create a very small variable. There is a 1 byte vartiable called a char that you can use. This is relatively impractical, since short ints aren't too big. So, what are chars used for? They are very useful for storing letters. If you have not heard of ASCII, it is basically a binary alphabet containing all numbers, english letters, many special characters, such as $, #, &, ç, ø, along with other characters that you can't see, such as space, return, etc. One special thing about ASCII is that everything is only 1 byte. Also, evry value corresponds to a number, so a has a value of 97, b=98, etc. */ //Arrays int myInt15; /* I'm not sure if arrays are technically considered variables, but they are closely related to chars, so I will explain them. Arrays can contain many values and of different types. In this case, it contains 15 ints. You can access any of the values in the array by doing: myInt3 In this case, I am taking the fourth value of this array. Fourth because its starts at myInt0. to be continued...